home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / qrt.lzh / INOUT.C < prev    next >
C/C++ Source or Header  |  1989-02-16  |  24KB  |  1,119 lines

  1.  
  2. /**********************************************************
  3.  
  4.                     object loader language
  5.  
  6.  **********************************************************/
  7.  
  8. #define IODEBUG 1
  9.  
  10. #include "qrt.h"
  11. #include "pattern.h"
  12.  
  13. OBJ_PTR      new_obj(), Get_Object(), Get_Primitive(),
  14.              Get_Instance_Of();
  15. PATTERN_PTR  Attach_Pattern(), Get_Pattern();
  16. char         *malloc();
  17.  
  18. # ifdef AMIGA
  19.     float      atof();
  20. # endif
  21.  
  22. /**********************************************************
  23.  
  24.              External declarations from lexer
  25.  
  26.  **********************************************************/
  27.  
  28. float IsPos();
  29. float Get_Next_Num();
  30. short Get_Color_Val();
  31. char *Get_Next_Name();
  32.  
  33.  
  34.  
  35. /**********************************************************
  36.  
  37.    Global input line number.  If negative, error rountines
  38.    will not report line number (already reached EOF).
  39.  
  40.  **********************************************************/
  41.  
  42. extern int linenumber;
  43.  
  44. /**********************************************************
  45.  
  46.           Load the entire world from standard input
  47.  
  48.  **********************************************************/
  49.  
  50. int LoadWorld()                      /* load world from stdio */
  51. {
  52.   linenumber = 1;
  53.   if((THEWORLD.stack=Get_Object())==NULL)
  54.     Error(ILLEGAL_OBJECT,201);
  55.  
  56.   linenumber = -1;
  57.   return(TRUE);
  58. }
  59.  
  60. /**********************************************************
  61.  
  62.      Get an object and return a pointer to it. If it is
  63.      not an object, but some other world attribute
  64.      specification, note it, but keep trying until an
  65.      object is found or end of input is encountered.
  66.      This is slighly hacked out, but it works for now.
  67.      It calls itself recursively sometimes.
  68.  
  69.  **********************************************************/
  70.  
  71. OBJ_PTR Get_Object() {
  72.   char str[SLEN], *name;
  73.   OBJ_PTR newobj, queue, temp;
  74.   VECTOR loc,rad,d,v3, upper, lower;
  75.   CINFO cinfo;
  76.   int found;
  77.  
  78.   name=NULL; queue=NULL;
  79.   VectEqZero(&loc);
  80.   VectEqZero(&rad);
  81.   VectEqZero(&d);
  82.   VectEqZero(&v3);
  83.   VectEqZero(&upper);
  84.   VectEqZero(&lower);
  85.  
  86.   if (feof(stdin)) return(NULL);
  87.   while (!feof(stdin)) {
  88.     GetToken(str); found = FALSE;
  89.  
  90. #   ifdef IODEBUG
  91.       printf("GETOBJECT: token=%s\n",str);
  92. #   endif
  93.  
  94.     if (GetAttrib(str))
  95.       found = TRUE;
  96.  
  97.     if (strcmp(str,"BEGIN_INSTANCES")==0) {
  98.       found = TRUE;
  99.       THEWORLD.instances = Get_Object();
  100.     }
  101.  
  102.     if (strcmp(str,"END_INSTANCES")==0) {
  103. #     ifdef ROBUST
  104.         if (queue==NULL) Error(SYNTAX_ERROR,205);
  105. #     endif
  106.       found = TRUE;
  107.       return(queue);
  108.     }
  109.  
  110.     if (strcmp(str,"END_BBOX")==0) {
  111. #     ifdef ROBUST
  112.         if (queue==NULL) Error(SYNTAX_ERROR,206);
  113. #     endif
  114.       found = TRUE;
  115.       return(queue);
  116.     }
  117.  
  118.     if (strcmp(str,"BEGIN_BBOX")==0) {
  119.  
  120.        newobj=new_obj(BBOX,&loc,&rad,&d,&v3,&cinfo,NULL,NULL,name,
  121.                       &upper, &lower,
  122.                       (float)0.0, (float)0.0, (float)0.0);
  123.  
  124.        newobj->child = Get_Object();
  125.        newobj->nextobj = queue;
  126.        queue = newobj;
  127.        found = TRUE;
  128.  
  129.     }
  130.  
  131.     if (strcmp(str,"NAME")==0) {
  132.        name=Get_Next_Name();
  133.        found = TRUE;
  134.     }
  135.  
  136.     if ((temp=Get_Primitive(str))!=NULL) {
  137.       found = TRUE;
  138.       temp->nextobj=queue;
  139.       queue = temp;
  140.     }
  141.  
  142.     if (!found && !feof(stdin)) Error(SYNTAX_ERROR,207);
  143.  
  144.   }
  145.  
  146.   return(queue);
  147. }
  148.  
  149.  
  150. /**********************************************************
  151.  
  152.                  Load default color info
  153.  
  154.  **********************************************************/
  155.  
  156. def_colorinfo(cinfo)
  157.   CINFO_PTR cinfo;
  158. {
  159.   copy_colorinfo(cinfo,&(def.cinfo));
  160. }
  161.  
  162.  
  163. /**********************************************************
  164.  
  165.                     Copy color info
  166.  
  167.  **********************************************************/
  168.  
  169. copy_colorinfo(c1,c2)
  170.   CINFO_PTR c1,c2;
  171. {
  172.   if (c1 == NULL) return;
  173.  
  174.   SVectEQ(&(c1->amb),&(c2->amb));
  175.   SVectEQ(&(c1->diff),&(c2->diff));
  176.   SVectEQ(&(c1->mirror),&(c2->mirror));
  177.   SVectEQ(&(c1->trans),&(c2->trans));
  178.   VectEQ(&(c1->density),&(c2->density));
  179.  
  180.   c1->fuzz     = c2->fuzz;
  181.   c1->index    = c2->index;
  182.   c1->dither   = c2->dither;
  183.   c1->sreflect = c2->sreflect;
  184.   c1->reflect  = c2->reflect;
  185. }
  186.  
  187.  
  188. /**********************************************************
  189.  
  190.             Get defaults from input file
  191.  
  192.  **********************************************************/
  193.  
  194. int Get_Default()
  195. {
  196.   char        str[SLEN];
  197.   int         end, found;
  198.  
  199.   end=0;
  200.  
  201.   GetLeftParen();
  202.  
  203.   while (!end && !feof(stdin)) {
  204.     GetToken(str);
  205.  
  206. #   ifdef IODEBUG
  207.       printf("GETDEFAULT : token=%s\n",str);
  208. #   endif
  209.  
  210.     found = GetOpt(str,&(def.cinfo));
  211.  
  212.     if (strcmp(str,"NO_SHADOW")==0) {
  213.       def.shadow = FALSE;
  214.       found = TRUE;
  215.     }
  216.  
  217.     if (strcmp(str,"NO_LAMP")==0) {
  218.       def.vlamp = FALSE;
  219.       found = TRUE;
  220.     }
  221.  
  222.     if (strcmp(str,"THRESHOLD")==0) {
  223.       def.threshold = IsPos(Get_Next_Num());
  224.       found = TRUE;
  225.     }
  226.  
  227.     if (strcmp(str,")")==0) { end = 1; found = TRUE;}
  228.  
  229.     if (!found) Error(UNDEFINED_PARAM,209);
  230.   }
  231.   return(TRUE);
  232. }
  233.  
  234.  
  235. /**********************************************************
  236.  
  237.                     Get object options
  238.  
  239.  **********************************************************/
  240.  
  241. int GetOpt(str,cinfo)
  242.   char str[];
  243.   CINFO_PTR cinfo;
  244. {
  245.   int found;
  246.  
  247.   found = FALSE;
  248.  
  249.   if (strcmp(str,"AMB")==0) {
  250.     GetSVector(&(cinfo->amb));
  251.     found = TRUE;
  252.   }
  253.  
  254.   if (strcmp(str,"DIFF")==0) {
  255.     GetSVector(&(cinfo->diff));
  256.     found = TRUE;
  257.   }
  258.  
  259.   if (strcmp(str,"MIRROR")==0) {
  260.     GetSVector(&(cinfo->mirror));
  261.     found = TRUE;
  262.   }
  263.  
  264.   if (strcmp(str,"TRANS")==0) {
  265.     GetSVector(&(cinfo->trans));
  266.     found = TRUE;
  267.   }
  268.  
  269.   if (strcmp(str,"DENSITY")==0) {
  270.     GetVector(&(cinfo->density));
  271.     found = TRUE;
  272.   }
  273.  
  274.   if (strcmp(str,"FUZZ")==0) {
  275.     cinfo->fuzz     = Get_Color_Val();
  276.     found = TRUE;
  277.   }
  278.  
  279.   if (strcmp(str,"INDEX")==0) {
  280.     cinfo->index    = IsPos(Get_Next_Num());
  281.     found = TRUE;
  282.   }
  283.   if (strcmp(str,"DITHER")==0) {
  284.     cinfo->dither   = (short)IsPos(Get_Next_Num());
  285.     found = TRUE;
  286.   }
  287.   if (strcmp(str,"SREFLECT")==0) {
  288.     cinfo->sreflect = IsPos(Get_Next_Num());
  289.     found = TRUE;
  290.   }
  291.   if (strcmp(str,"REFLECT")==0) {
  292.     cinfo->reflect = Get_Color_Val();
  293.     found = TRUE;
  294.   }
  295.  
  296.   return(found);
  297. }
  298.  
  299.  
  300. /**********************************************************
  301.  
  302.           Load a sphere and return pointer to it
  303.  
  304.  **********************************************************/
  305.  
  306. OBJ_PTR GetSphere()
  307. {
  308.   char        str[SLEN], *name;
  309.   VECTOR      loc, rad, d, v3, upper, lower;
  310.   float       xmult, ymult;
  311.   CINFO       cinfo;
  312.   OBJ_PTR     newobj;
  313.   PATTERN_PTR pattern, remove;
  314.   int         end, f, found;
  315.  
  316.   end=f=0;
  317.   def_colorinfo(&cinfo);
  318.   xmult=ymult=1;
  319.   pattern = remove = NULL;
  320.   name    = NULL;
  321.  
  322.   GetLeftParen();
  323.  
  324.   while (!end && !feof(stdin)) {
  325.     GetToken(str);
  326.  
  327. #   ifdef IODEBUG
  328.       printf("GETSPHERE : token=%s\n",str);
  329. #   endif
  330.  
  331.     found = GetOpt(str,&cinfo);
  332.  
  333.     if ((strcmp(str,"POS")==0)      ||
  334.         (strcmp(str,"LOC")==0)      ||
  335.         (strcmp(str,"POSITION")==0) ||
  336.         (strcmp(str,"LOCATION")==0)) {
  337.  
  338.       GetVector(&loc);
  339.       f|=1; found = TRUE;
  340.     }
  341.  
  342.     if (strcmp(str,"RADIUS")==0) {
  343.       rad.x = IsPos(Get_Next_Num());
  344.       f|=2; found = TRUE;
  345.     }
  346.     if (strcmp(str,"PATTERN")==0) {
  347.       pattern = Attach_Pattern();
  348.       found = TRUE;
  349.     }
  350.     if (strcmp(str,"REMOVE")==0) {
  351.       remove = Attach_Pattern();
  352.       found = TRUE;
  353.     }
  354.     if (strcmp(str,"XMULT")==0) {
  355.       xmult = IsPos(Get_Next_Num());
  356.       found=TRUE;
  357.     }
  358.     if (strcmp(str,"YMULT")==0) {
  359.       ymult = IsPos(Get_Next_Num());
  360.       found=TRUE;
  361.     }
  362.     if (strcmp(str,"NAME")==0) {
  363.        name=Get_Next_Name();
  364.        found = TRUE;
  365.     }
  366.  
  367.     if (strcmp(str,")")==0) { end = 1; found = TRUE; }
  368.  
  369.     if (!found) Error(UNDEFINED_PARAM,211);
  370.   }
  371.  
  372.   if (f!=3) Error(TOO_FEW_PARMS,212);
  373.   rad.y=rad.z=0;
  374.  
  375.   VectEqZero(&d);
  376.   VectEqZero(&upper);
  377.   VectEqZero(&lower);
  378.  
  379.   newobj=new_obj(SPHERE,&loc,&rad,&d,&v3,&cinfo,pattern,
  380.                  remove,name,
  381.                  &upper, &lower, 0.0, xmult, ymult);
  382.  
  383.   THEWORLD.objcount++;
  384.   return(newobj);
  385. }
  386.  
  387.  
  388. /**********************************************************
  389.  
  390.              Load lamp and attach to world
  391.  
  392.  *************